﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using AutoMapper;
using DTO;

namespace DAO
{
    public class ChiTietChuyenBay_DAO
    {
        public static ChiTietChuyenBay_DTO CreateChiTietChuyenBayDTO(ChiTietChuyenBay chitietchuyenbay)
        {
            ChiTietChuyenBay_DTO chitietchuyenbayDTO = new ChiTietChuyenBay_DTO();
            Mapper.CreateMap<ChiTietChuyenBay, ChiTietChuyenBay_DTO>();
            chitietchuyenbayDTO = Mapper.Map<ChiTietChuyenBay, ChiTietChuyenBay_DTO>(chitietchuyenbay);
            return chitietchuyenbayDTO;
        }

        public static ChiTietChuyenBay CreateChiTietChuyenBayEFF(ChiTietChuyenBay_DTO chitietchuyenbayDTO)
        {
            ChiTietChuyenBay chitietchuyenbay = new ChiTietChuyenBay();
            Mapper.CreateMap<ChiTietChuyenBay_DTO, ChiTietChuyenBay>();
            chitietchuyenbay = Mapper.Map<ChiTietChuyenBay_DTO, ChiTietChuyenBay>(chitietchuyenbayDTO);
            return chitietchuyenbay;
        }

        public List<ChiTietChuyenBay_DTO> GetChiTietChuyenBayList()
        {
            List<ChiTietChuyenBay_DTO> listChiTietChuyenBay = new List<ChiTietChuyenBay_DTO>();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from chuyenbay in context.ChiTietChuyenBays select chuyenbay;
                foreach (var cb in query)
                {
                    ChiTietChuyenBay_DTO chitietchuyenbayDTO = CreateChiTietChuyenBayDTO(cb);
                    listChiTietChuyenBay.Add(chitietchuyenbayDTO);
                }
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
            }
            return listChiTietChuyenBay;
        }

        public BindingList<ChiTietChuyenBay_DTO> GetChiTietChuyenBayDetails(string mMaChuyenBay)
        {
            BindingList<ChiTietChuyenBay_DTO> listchitietchuyenbayDTO = new BindingList<ChiTietChuyenBay_DTO>();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from ctcb in context.ChiTietChuyenBays
                            where ctcb.MaChuyenBay == mMaChuyenBay
                            select ctcb;
                int i = 1;
                foreach (var cb in query)
                {
                    ChiTietChuyenBay_DTO chitietchuyenbayDTO = CreateChiTietChuyenBayDTO(cb);
                    chitietchuyenbayDTO.STT = i;
                    listchitietchuyenbayDTO.Add(chitietchuyenbayDTO);
                    i++;
                }
            }
            catch (System.Exception)
            {
            }
            return listchitietchuyenbayDTO;
        }

        public static string MaChiTietChuyenBayMax()
        {
            QLCBDataContext context = new QLCBDataContext();
            string query = (from cb in context.ChiTietChuyenBays select cb.MaChiTietChuyenBay).Max();
            return query;
        }

        public void InsertChiTietChuyenBay(ChiTietChuyenBay_DTO chitietchuyenbayDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                ChiTietChuyenBay chitietchuyenbay = new ChiTietChuyenBay();//CreateChiTietChuyenBayEFF(chitietchuyenbayDTO);
                chitietchuyenbay.MaChiTietChuyenBay = chitietchuyenbayDTO.MaChiTietChuyenBay;
                chitietchuyenbay.MaChuyenBay = chitietchuyenbayDTO.MaChuyenBay;
                chitietchuyenbay.SanBayTrungGian = chitietchuyenbayDTO.SanBayTrungGian;
                chitietchuyenbay.ThoiGianDung = chitietchuyenbayDTO.ThoiGianDung;
                chitietchuyenbay.GhiChu = chitietchuyenbayDTO.GhiChu;
                context.ChiTietChuyenBays.InsertOnSubmit(chitietchuyenbay);
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void InsertNhieuChiTiet(BindingList<ChiTietChuyenBay_DTO> listChiTiet)
        {
            try
            {
                foreach (ChiTietChuyenBay_DTO item in listChiTiet)
                {
                    InsertChiTietChuyenBay(item);
                }
            }
            catch(System.Exception ex)
            {
                string tr = ex.Message;
            }
        }

        public void UpdateChiTiet(ChiTietChuyenBay_DTO chitietchuyenbayDTO)
        {
            try
            { 
                QLCBDataContext context = new QLCBDataContext();
                ChiTietChuyenBay chitietchuyenbay = context.ChiTietChuyenBays.Single(ct => ct.MaChiTietChuyenBay == chitietchuyenbayDTO.MaChiTietChuyenBay);
                chitietchuyenbay.MaChiTietChuyenBay = chitietchuyenbayDTO.MaChiTietChuyenBay;
                chitietchuyenbay.SanBayTrungGian = chitietchuyenbayDTO.SanBayTrungGian;
                chitietchuyenbay.ThoiGianDung = chitietchuyenbayDTO.ThoiGianDung;
                chitietchuyenbay.GhiChu = chitietchuyenbayDTO.GhiChu;
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void DeleteChiTiet(ChiTietChuyenBay_DTO chitietchuyenbayDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                ChiTietChuyenBay chitietchuyenbay = context.ChiTietChuyenBays.Single(ct => ct.MaChiTietChuyenBay == chitietchuyenbayDTO.MaChiTietChuyenBay);
                context.ChiTietChuyenBays.DeleteOnSubmit(chitietchuyenbay);
                context.SubmitChanges();
            }
            catch
            { }
        }

    }
}
